package com.cloudsigns.factorymanager2.media_system.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.media_system.fragment.MediaSystemReceiveOrderAlreadyFragment;
import com.cloudsigns.factorymanager2.media_system.fragment.MediaSystemReceiveOrderBackFragment;
import com.cloudsigns.factorymanager2.media_system.fragment.MediaSystemReceiveOrderWaitingFragment;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;


public class MediaSystemReceiveOrderFragmentActivity extends FragmentActivity implements View.OnClickListener {
    private final String TAG = MediaSystemReceiveOrderFragmentActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private View ll_waiting_receive, ll_already_received, ll_back;
    private TextView tv_waiting_receive, tv_already_received, tv_back;
    private CustomViewPager viewpager;
    private View rl_progressbar;
    public static final int WATTING_RECEIVE = 0;
    public static final int ALREADY_RECEIVED = 1;
    public static final int BACK = 2;
    public int currentIndex = WATTING_RECEIVE;

    private MediaSystemReceiveOrderWaitingFragment waitingFragment;
    private MediaSystemReceiveOrderAlreadyFragment alreadyFragment;
    private MediaSystemReceiveOrderBackFragment backFragment;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_produce_receive_order);


        initView();

        tv_title.setText(getResources().getText(R.string.receive_order));
        EventBus.getDefault().register(this);// 注册

        initData();
    }

    private void initView() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        viewpager = (CustomViewPager) findViewById(R.id.viewpager);
        ll_waiting_receive = findViewById(R.id.ll_waiting_receive);
        ll_already_received = findViewById(R.id.ll_already_received);
        ll_back = findViewById(R.id.ll_back);
        tv_waiting_receive = findViewById(R.id.tv_waiting_receive);
        tv_already_received = findViewById(R.id.tv_already_received);
        tv_back = findViewById(R.id.tv_back);

        rl_back.setOnClickListener(this);
        ll_waiting_receive.setOnClickListener(this);
        ll_already_received.setOnClickListener(this);
        ll_back.setOnClickListener(this);

        waitingFragment = new MediaSystemReceiveOrderWaitingFragment();
        alreadyFragment = new MediaSystemReceiveOrderAlreadyFragment();
        backFragment = new MediaSystemReceiveOrderBackFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(waitingFragment);
        listFragments.add(alreadyFragment);
        listFragments.add(backFragment);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);

        PageIndicatorView indicator = (PageIndicatorView) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_2E75B6));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 从服务器获取数据
     */
    private void initData() {

    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            // 等待接收
            case WATTING_RECEIVE:
                ll_waiting_receive.setSelected(true);
                ll_already_received.setSelected(false);
                ll_back.setSelected(false);

                tv_waiting_receive.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_already_received.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_back.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 已经接收
            case ALREADY_RECEIVED:
                ll_waiting_receive.setSelected(false);
                ll_already_received.setSelected(true);
                ll_back.setSelected(false);

                tv_waiting_receive.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_already_received.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_back.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 退回
            case BACK:
                ll_waiting_receive.setSelected(false);
                ll_already_received.setSelected(false);
                ll_back.setSelected(true);

                tv_waiting_receive.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_already_received.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_back.setTextColor(getResources().getColor(R.color.rgb_3333333));
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 等待接收
            case R.id.ll_waiting_receive:
                if (currentIndex != WATTING_RECEIVE) {
                    currentIndex = WATTING_RECEIVE;
                    viewpager.setCurrentItem(WATTING_RECEIVE);
                    showSelectedOption(WATTING_RECEIVE);
                }
                break;
            // 已经接收
            case R.id.ll_already_received:
                if (currentIndex != ALREADY_RECEIVED) {
                    currentIndex = ALREADY_RECEIVED;
                    viewpager.setCurrentItem(ALREADY_RECEIVED);
                    showSelectedOption(ALREADY_RECEIVED);
                }
                break;
            //　退回
            case R.id.ll_back:
                if (currentIndex != BACK) {
                    currentIndex = BACK;
                    viewpager.setCurrentItem(BACK);
                    showSelectedOption(BACK);
                }
                break;
        }
    }

    public void onEventMainThread(ProduceForTeamEvent event) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//解注册
    }
}
